package com.example.maxboxtwo.components.function.trumpet;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.maxboxtwo.R;

import static android.graphics.BlendMode.COLOR;

public class TrumpetView extends View {
    private Paint paint;
    private Bitmap bitmap_bg;
    private Bitmap bitmap_pointer;
    private Bitmap bitmap_display;
    private int voiceVolume = 0;


    public TrumpetView(Context context) {
        super(context);
    }

    public TrumpetView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TrumpetView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getVoiceVolume() {
        return voiceVolume;
    }

    public void setVoiceVolume(int voiceVolume) {
        this.voiceVolume = voiceVolume;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setTextSize(30);
        paint.setColor(Color.BLACK);

        canvas.drawBitmap(bitmap_bg,0,(getHeight()-bitmap_bg.getHeight())/2,paint);
        //canvas.save() and canvas.restore() can store canvas's status ago ,can't influence the next canvas's motion
        canvas.save();
        canvas.rotate(getVoiceVolume(),getWidth()/2-25,(getHeight()+bitmap_bg.getHeight())/2-20);
        canvas.drawBitmap(bitmap_pointer,getWidth()/2-bitmap_pointer.getWidth()-bitmap_display.getWidth()/2,(getWidth()+bitmap_pointer.getWidth())/2-15,paint);
        canvas.restore();

        canvas.drawBitmap(bitmap_display,getWidth()/2-bitmap_display.getWidth()/2,(getWidth()+bitmap_display.getWidth())/2,paint);

        canvas.drawText((getVoiceVolume()/2)+"dB",getWidth()/2-25,(getHeight()+bitmap_bg.getHeight())/2-20,paint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        bitmap_bg = bitmap_bg.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.sound_background),w,(int)(0.6*h), true);


        bitmap_pointer = bitmap_pointer.createBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.sound_pointer));

        bitmap_display = bitmap_display.createBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.soundm_displaybg));
    }
}
